package cn.jiyun.controller;


import cn.jiyun.common.GeocodingService;
import cn.jiyun.common.config.Result;
import cn.jiyun.mapper.SblxMapper;
import cn.jiyun.pojo.Fw;
import cn.jiyun.pojo.Sbgl;
import cn.jiyun.pojo.*;
import cn.jiyun.service.DwService;
import cn.jiyun.service.FwService;
import cn.jiyun.service.SbglService;
import cn.jiyun.service.SblxService;
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.joda.time.DateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.InputStream;
import java.util.*;

@RestController
@RequestMapping("fw")
@CrossOrigin
public class FwController {
    @Autowired
    private FwService fwService;

    @Autowired
    private SbglService sbglService;
    @Autowired
    private SblxMapper sblxMapper;
    @Autowired
    private GeocodingService geocodingService;
    @Autowired
    private DwService dwService;

    @RequestMapping("/findPage")
//    @AutoLogs(value = "服务管理查询操作", loggingtype  = 0)
    public Result findPage(Integer pageNum, Integer pageSize, @RequestBody Fw fw){
        Page page=fwService.findPage(pageNum,pageSize,fw);
        return Result.ok(page);
    }

    @RequestMapping("sbList")
    public Result sbList(){
        List<Sblx> sbList=sblxMapper.selectList(null);
        return Result.ok(sbList);
    }


    @RequestMapping("findById")
    public Result findById(Integer id){
        Fw fw=fwService.findById(id);
        return Result.ok(fw);
    }

    @RequestMapping("/add")
//    @AutoLogs(value = "服务管理添加操作", loggingtype  = 0)
    public Result add(@RequestBody Fw fw){
        if (fw.getId()==null){
            //关联地图的添加
            Dw dw = new Dw();
            dw.setAddress(fw.getDz());
            GeoResult geocode = geocodingService.getGeocode(fw.getDz());
            String[] split = geocode.getLocation().split(",");
            fw.setDimensionality(Double.parseDouble(split[0]));
            fw.setLongitude(Double.parseDouble(split[1]));
            dw.setStatus("启动");
            dwService.save(dw);

            //本身的服务管理的添加
            fw.setAge(22);
            /*fw.setBdsb("电子商品");*/
            /*fw.setWid(2);*/
            fw.setSid(1);
            fw.setBindingState(1);
            fw.setDepartment("服务部");
            fw.setDid(dw.getId());
            fwService.save(fw);

        }else{
            fwService.updateById(fw);
        }
        return Result.ok();
    }

    @RequestMapping("toUpdate")
//    @AutoLogs(value = "服务管理修改操作", loggingtype  = 0)
    public Result toUpdate(Integer id){
        Fw fw=fwService.getById(id);
        return Result.ok(fw);
    }


    @RequestMapping("deleteById")
//    @AutoLogs(value = "服务管理删除操作", loggingtype  = 0)
    public Result deleteById(Integer id){
        fwService.removeById(id);
        return Result.ok();
    }

    @Value("${aliyun.endpoint}")
    private String endpoint;
    @Value("${aliyun.keyid}")
    private String keyid;
    @Value("${aliyun.keysecret}")
    private String keysecret;
    @Value("${aliyun.bucketname}")
    private String bucketname;

    @RequestMapping("tp")
//    @AutoLogs(value = "服务管理图片上传操作", loggingtype  = 0)
    public String fileUpload(MultipartFile file) {

        String url = "";
        // 创建OSSClient实例。
        OSS ossClient = new OSSClientBuilder().build(endpoint, keyid, keysecret);
        try {
            // 创建PutObjectRequest对象。
            InputStream inputStream = file.getInputStream();
            String originalFilename = file.getOriginalFilename();

            String replace = UUID.randomUUID().toString().replace("-", "");
            originalFilename = replace + originalFilename;
            String time = new DateTime().toString("yyyy/MM/dd");
            originalFilename = time + "/" + originalFilename;

            PutObjectResult putObjectResult = ossClient.putObject(bucketname, originalFilename, inputStream);

            url = "https://" + bucketname + "." + endpoint + "/" + originalFilename;
        } catch (Exception ce) {
            System.out.println("上传异常--");
        } finally {
            if (ossClient != null) {
                ossClient.shutdown();
            }
        }
        return url;
    }






    @RequestMapping("getInfo")
    public Result xy(Integer id){
        QueryWrapper<Sbgl> qr=new QueryWrapper<>();
        qr.eq("yid",id);
        List<Sbgl> list=sbglService.list(qr);
        return Result.ok(list);

    }



  /*  @GetMapping("/echarts/bie")
    public Result bar(){
        List<Fw> list=fwService.findAll();
        Map<String,Long> collect=list.stream()
                .filter(x->!ObjectUtils.isEmpty(x.getXname()))
                .collect(Collectors.groupingBy(Fw::getXname,Collectors.counting()));
        List<Map<String,Object>> mapList=new ArrayList<>();
        if (CollectionUtils.isNotEmpty(collect)){
            for (String key : collect.keySet()){
                Map<String,Object> map=new HashMap<>();
                map.put("name",key);
                map.put("value",collect.get(key));
                mapList.add(map);
            }
        }
        return Result.ok(mapList);

    }*/


    /*@GetMapping("/echarts/line")
    public Result bar(){
        List<Fw> list=fwService.findAll();
        Map<String,Long> collect=list.stream()
                .filter(x->!ObjectUtils.isEmpty(x.getXname()))
                .collect(Collectors.groupingBy(Fw::getXname,Collectors.counting()));

        List<String> xAxis = new ArrayList<>();
        List<Long> yAxis = new ArrayList<>();
        if (CollectionUtils.isNotEmpty(collect)){
            for (String key : collect.keySet()){
                xAxis.add(key);
                yAxis.add(collect.get(key));
            }
        }
        Map<String,Object> map=new HashMap<>();
        map.put("xAxis",xAxis);
        map.put("yAxis",yAxis);

        return Result.ok();

    }*/














}
